﻿using Common.Contract.Authentication.Record;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveChecklistCustomerByCcidServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveChecklistCustomerByCcidRequest, RetrieveChecklistCustomerByCcidResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        private List<ChecklistCustomerRecord> ListCustomer = new List<ChecklistCustomerRecord>();
        public int Ccid;
        #endregion
        public RetrieveChecklistCustomerByCcidServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveChecklistCustomerByCcidRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Ccid == 0)
            {
                throw new ArgumentException("Product must be set", "request");
            }
            else
            {
                Ccid = request.Ccid;
            }

            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            TextSearch = request.Filter;
        }

        protected override bool OnExecute(RetrieveChecklistCustomerByCcidResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                ChecklistCustomerRecord record = new ChecklistCustomerRecord();
                ChecklistCustomer listCustomer = new ChecklistCustomer();
                listCustomer = Providers.ProductHandbooks.RetrieveCustomerChecklistByCcid(Ccid, Dao);
                List<ChecklistDocumentByCustomer> listDocumentByCcid = Providers.ProductHandbooks.RetrieveListDocumentByCcid(out totalRecord, Ccid, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
                if (listCustomer != null)
                {
                    record = new ChecklistCustomerRecord
                    {
                        Ccid = listCustomer.Ccid,
                        Branch = listCustomer.Branch,
                        CustName = listCustomer.CustName,
                        Dao = listCustomer.Dao,
                        DaoName = listCustomer.DaoName,
                        DateAdd = listCustomer.DateAdd,
                        DateUpdate = listCustomer.DateUpdate,
                        LegalId = listCustomer.LegalId,
                        ProductId = listCustomer.ProductId,
                        Region = listCustomer.Region,
                        Cif = listCustomer.Cif,
                        SubProductId = listCustomer.SubProductId,
                        SubProductName = listCustomer.SubProductName,
                        Checked = listDocumentByCcid.Where(x => x.Status == 1).Count().ToString(),
                        Unchecked = listDocumentByCcid.Where(x => x.Status == 0).Count().ToString()
                    };
                }

                response.Record = record;
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;
            }
            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
